﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Diagnostics;

namespace SCOSub
{
    class ScanMemAddress
    {
        [DllImport("kernel32.dll")]
        public static extern int OpenProcess(
            int dwDesiredAccess,
            int bInheritHandle,
            int dwProcessId
        );
        [DllImport("kernel32.dll", EntryPoint = "CloseHandle")]
        public static extern int CloseHandle(
            int hObject
        );
        [DllImport("kernel32.dll")]
        public static extern Int32 ReadProcessMemory(
            IntPtr hProcess,
            IntPtr lpBaseAddress,
            byte[] buffer,
            int size,
            out int lpNumberOfBytesRead
        );

        public int scan()
        {
            int PID;
            try
            {
                PID = Process.GetProcessesByName("WaveEdit")[0].Id;
            }
            catch
            {
                
                return -1;
            }
            int hprocess = OpenProcess(0x1F0FFF, 0, PID);
            int num;
            /*
            byte[] buffer0 = new byte[0x80020];
            
            int addressbox1 = 0;

            for (int addressbox = 0x10000; addressbox <= int.MaxValue; addressbox += 0x80000)
            {
                ReadProcessMemory((IntPtr)hprocess, (IntPtr)addressbox, buffer0, 0x80020, out num);
                string s = Encoding.Default.GetString(buffer0);
                if (s.IndexOf("已选取") != -1)
                {
                    addressbox1 = addressbox;
                    break;
                }
            }
            */
            byte[] buffer = new byte[0x1020];
            
            int addressbox2 = 0;
            //int pass2end = addressbox1 + 0x80020;
            for (int addressbox = 0x10000; addressbox <= int.MaxValue; addressbox += 0x1000)
            {
                ReadProcessMemory((IntPtr)hprocess, (IntPtr)addressbox, buffer, 0x1020, out num);
                string s = Encoding.Default.GetString(buffer);
                if (s.IndexOf("已选取") != -1)
                {
                    addressbox2 = addressbox;
                    break;
                }
            }
            byte[] buffer2 = new byte[6];
            int finaladdress = 0;
            int pass3end = addressbox2 + 0x1020;
            for (int i = addressbox2; i <= pass3end; i++)
            {
                ReadProcessMemory((IntPtr)hprocess, (IntPtr)i, buffer2, 6, out num);
                string s = Encoding.Default.GetString(buffer2);
                if (s.IndexOf("已选取") != -1)
                {
                    finaladdress = i;
                    break;
                }
            }
            CloseHandle(hprocess);
            
            return finaladdress;

        }

    }
}
